from pathlib import  Path
import time
import sys
import os
import getopt

if __name__ == "__main__":
    dir_str = '/home/chengyu/wallpapers/paper_one/'
    xml_str = 'my_wp_cfg.xml'
    static_duration = '300'
    trans_duration = '3'

    argv = sys.argv
    help_msg = 'usage:gen_wp_cfg.py -d <wallPaperDir>  -s <static_duration> -t <trans_duration>' \
               '--xml_name <xml_cfg_name> (-h --help ;help doc)'
    try:
        opts, args = getopt.getopt(argv[1:], "d:s:t:h", ['help', 'xml_name='])
    except getopt.GetoptError:
        print(1)
        print(help_msg)
    for opt, val in opts:
        print("opt:"+opt)
        print("val:"+val)
        if opt in ('-h', '--help'):
            print(help_msg)
        elif opt == '-d' and os.path.exists(val):
            dir_str = val
        elif opt == '-s' and val.isdecimal():
            static_duration = val
        elif opt == '-t' and val.isdecimal():
            trans_duration = val
        elif opt == '--xml_name' and val.endswith('.xml'):
            xml_str = val
        else:
            # print(2)
            # print(opt)
            print(help_msg)
            exit(1)

    dir_p = Path(dir_str)

    if not dir_p.exists() or dir_p.is_file():
        print('此路径不存在')

    file_list = []
    for child in dir_p.iterdir():
        if child.suffix in [".jpg", ".jpeg", ".png", ".bmp"]:
            c_name = child.name
            full_name = dir_str + c_name
            file_list.append(full_name)

    local_t = time.localtime(time.time())

    start_time_str = "\t<starttime>\n" \
                     "\t\t<year>{0}</year>\n" \
                     "\t\t<month>{1}</month>\n" \
                     "\t\t<day>{2}</day>\n" \
                     "\t\t<hour>{3}</hour>\n" \
                     "\t\t<minute>{4}</minute>\n" \
                     "\t\t<second>{5}</second>\n" \
                     "\t</starttime>\n"

    static_transition_str = "\t<static>\n" \
                            "\t\t<duration>{0}</duration>\n" \
                            "\t\t<file>{1}</file>\n" \
                            "\t</static>\n" \
                            "\t<transition>\n" \
                            "\t\t<duration>{2}</duration>\n" \
                            "\t\t<from>{1}</from>\n" \
                            "\t\t<to>${3}</to>\n" \
                            "\t</transition>\n"

    with open(xml_str, 'w', encoding='utf-8') as xml_file:
        xml_file.write("<background>\n")

        tmp_str = start_time_str.format(local_t.tm_year, local_t.tm_mon, local_t.tm_mday,
                                               local_t.tm_hour, local_t.tm_min, local_t.tm_sec)
        xml_file.write(tmp_str)

        tmp_len = len(file_list)

        for i in range(tmp_len):
            tmp_str = static_transition_str.format(static_duration, file_list[i], trans_duration,
                                                   file_list[(i+1) % tmp_len])
            xml_file.write(tmp_str)

        xml_file.write('</background>')

    print('xml壁纸配置文件生成成功！！！')
    exit(0)



